package com.szosen.util;

import java.util.LinkedHashMap;
import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.szosen.jkzj.R;

/**
 * 显示三列文本的适配器，设置第一行为标题行，背景与内容行不同
 */
public class ThreeCloumAdpterMBGY extends BaseAdapter {
	private static final int LENGHT = 3;
	private List<String[]> datas;
	private Context context;
	private LinkedHashMap<String, String> map;
	private Handler mHandler;
	public static final int WHAT_FAILED = 4;

	public ThreeCloumAdpterMBGY(Context context, List<String[]> datas,
			Handler handler) {
		this.context = context;
		this.datas = datas;
		map = new LinkedHashMap<String, String>();
		mHandler = handler;
	}

	@Override
	public int getCount() {
		return datas.size();
	}

	@Override
	public Object getItem(int position) {
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public boolean putData(String position, String data) {
		if (map!=null&& map.size() < 4) {
			map.put(position, data);
			return true;
		} else {
			return false;
		}
	}

	public boolean removeData(String position) {
		String s = map.remove(position);
		if (!TextUtils.isEmpty(s)) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final Holder holder;
		// 只有为空时才构造
		if (convertView == null) {
			holder = new Holder();
			holder.dataET = new TextView[LENGHT];
			LayoutInflater inflater = LayoutInflater.from(context);
			convertView = inflater.inflate(R.layout.list_item_all_record_mbgy,
					null);
			holder.dataET[0] = (TextView) convertView
					.findViewById(R.id.list_data_1);
			holder.dataET[1] = (TextView) convertView
					.findViewById(R.id.list_data_2);
			holder.dataET[2] = (TextView) convertView
					.findViewById(R.id.list_data_3);

			holder.layout = (LinearLayout) convertView
					.findViewById(R.id.record_layout);
			holder.checkBox = (CheckBox) convertView.findViewById(R.id.list_cb);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}

		if (position < datas.size()) {
			final String[] line = datas.get(position);
			holder.checkBox
					.setOnCheckedChangeListener(new OnCheckedChangeListener() {

						@Override
						public void onCheckedChanged(CompoundButton buttonView,
								boolean isChecked) {
							String time = line[3];
							if (isChecked) {
								boolean b = putData(position + "", time);
								if (!b) {
									mHandler.obtainMessage(WHAT_FAILED)
											.sendToTarget();
									holder.checkBox.setChecked(false);
								}
							} else {
								removeData(position + "");
							}

						}
					});

			for (int i = 0; i < LENGHT; i++) {
				if (line[i] == null) {
					continue;
				}
				String item;
				if (line[i].length() > 200)// 显示心电太长了,只显示部分
					item = line[i].substring(0, 20) + "...";
				else
					item = line[i];
				holder.dataET[i].setText(item);
			}
		}
		// holder.layout.setBackgroundResource(R.drawable.button_selector);
		/*
		 * if (position == 0) {
		 * holder.layout.setBackgroundResource(R.color.record_title); } else {
		 * // 设置内容栏的背景
		 * holder.layout.setBackgroundResource(R.drawable.button_selector); }
		 */
		return convertView;
	}

	/***
	 * 用于暂存数据
	 */
	class Holder {
		TextView[] dataET;// 显示的内容
		CheckBox checkBox;
		LinearLayout layout;// 设定背景
	}

	public LinkedHashMap<String, String> getMapData() {
		return map;
	}
}
